
* Load main dataset
clear
use "./federal-candidates-2021-12-14_prepped_2022-07-19.dta"

*Merge in the number of seconds given 
merge m:1 parliament id using "./seconder_frequency.dta"

gen seconder = (_merge == 3)
drop if _merge == 2
replace num_seconds = 0 if _merge == 1
drop _merge 


*Merge in the number of seconds received 
sort parliament bill motion
order parliament bill motion 
merge m:1 parliament bill motion using "./seconds_received.dta"

drop if _merge == 2
replace seconds_received = 0 if _merge == 1
drop _merge 



*variable indicating how many bills or motions an MP seconds in the previous parliament
sort id parliament 
by id: gen num_seconds_lag = num_seconds[_n - 1] if parliament[_n - 1] == parliament - 1


*avoid RHS missingness
gen miss_lag = 1
replace miss_lag = 0 if num_seconds_lag !=.

gen num_seconds_lag_m = num_seconds_lag
replace  num_seconds_lag_m = 99 if miss_lag ==1

replace P2P_previous = 99 if miss_lag == 1


* distribution of seconder 
tab parliament seconder 


		
*FIGURE 1: Total number of proposals and seconds in each parliament
preserve
clear
import delimited ./seconder_complete.csv, encoding(utf8) 
gen seconder_dy = 0 
replace seconder_dy = 1 if !missing(id)
gen bill_motion = bill + motion 
collapse (sum) seconder_dy, by(parliament bill_motion)
gen seconded = seconder_dy > 0 	
contract parliament seconded
bysort parliament: egen total_proposal = sum(_freq) 
* gen sitting days from parliament of canada 
* https://lop.parl.ca/sites/ParlInfo/default/en_CA/Parliament/parliamentsSessions
gen sitting_days = . 
replace sitting_days = 326 + 257 if parliament == 43
replace sitting_days = 1378 if parliament == 42
replace sitting_days = 655 + 834 if parliament == 41
replace sitting_days = 388 + 338 + 16 if parliament == 40
replace sitting_days = 327 + 529 if parliament == 39
replace sitting_days = 421 if parliament == 38
gen proposal_per_day = total_proposal / sitting_days
gen seconds_per_day = _freq / sitting_days 


graph twoway (line total_proposal parliament if seconded == 1) || ///
	(line _freq parliament if seconded == 1), ytitle("Number of Proposals") xtitle("Parliament") ///
	legend(label(1 "total") label(2 "seconded"))
	graph save "./num_proposal1.gph"

graph twoway (line proposal_per_day parliament if seconded == 1) || ///
	(line seconds_per_day parliament if seconded == 1), ytitle("Number of Proposals Per Day") xtitle("Parliament") ///
	legend(label(1 "total") label(2 "seconded"))
	graph save "./num_proposal2.gph"
grc1leg2 "num_proposal1" "num_proposal2", rows(2) ysize(8)
restore


		
*Table 1: Descriptive Statistics 		

gen bill_or_motion = 0 
replace bill_or_motion = 1 if !missing(bill__1_)

*Number of MPs who have P2P. See total for Total number of MPs
tab P2P
*Total number of MPs who make a proposal / Total number of proposals
tab bill_or_motion
*Number of proposals that receives a second
preserve 
clear
import delimited ./seconder_complete.csv, encoding(utf8) 
tostring parliament, gen(parl_str)
gen parl_bill_motion = parl_str+bill+motion 
*Number of proposals that receive a second
codebook parl_bill_motion if !missing(id)
restore
*Total number of seconds
preserve
contract num_seconds 
gen inner = num_seconds* _freq
egen suminner = sum(inner)
list suminner 
restore 
*Number of MPs who seconded at least one proposal 
tab seconder
*Number of proposals that passed 
tab passed2
*Number of proposals that passed with at least one second
tab year passed2 if num_seconds > 0


*See "analyses_dyad_replication" file for Table 2 and Table 3 


* Figure 2: Relationship between number of seconds given and place on list
label variable num_seconds "Number of Seconds"

twoway (scatter num_seconds placeonlist, mcolor(%40) msize(small)) ///
	(lowess num_seconds placeonlist, lcolor(red)) if parliament == 38, xline(87.5, lcolor(gs12)) title("38th Parliament") ///
	ytitle("Seconds Given") xtitle("Place on List") legend(label(1 "Number of Seconds")) legend(label(2 "Lowess"))
	graph save "./38.gph"
twoway (scatter num_seconds placeonlist, mcolor(%40) msize(small)) ///
	(lowess num_seconds placeonlist, ///
	lcolor(red)) if parliament == 39, xline(117.5, lcolor(gs12)) title("39th Parliament") ///
	ytitle("Seconds Given") xtitle("Place on List")
	graph save "./39.gph"
twoway (scatter num_seconds placeonlist, mcolor(%40) msize(small)) ///
	(lowess num_seconds placeonlist, lcolor(red)) if parliament == 40, xline(108.5, lcolor(gs12)) title("40th Parliament") ///
	ytitle("Seconds Given") xtitle("Place on List")
	graph save "./40.gph"
twoway (scatter num_seconds placeonlist, mcolor(%40) msize(small)) ///
	(lowess num_seconds placeonlist, lcolor(red)) if parliament == 41, xline(203.5, lcolor(gs12)) title("41st Parliament") ///
	ytitle("Seconds Given") xtitle("Place on List")
	graph save "./41.gph"
twoway (scatter num_seconds placeonlist, mcolor(%40) msize(small)) ///
	(lowess num_seconds placeonlist, lcolor(red)) if parliament == 42, xline(162.5, lcolor(gs12)) title("42nd Parliament") ///
	ytitle("Seconds Given") xtitle("Place on List")
	graph save "./42.gph"
twoway (scatter num_seconds placeonlist, mcolor(%40) msize(small)) ///
	(lowess num_seconds placeonlist, lcolor(red)) if parliament == 43, xline(61.5, lcolor(gs12)) title("43rd Parliament") ///
	ytitle("Seconds Given") xtitle("Place on List")
	graph save "./43.gph"
grc1leg2 "38" "39" "40" "41" "42" "43", ycommon xcommon 

*stat on example candidate in 38th parliament
list candidate_name placeonlist num_seconds party if parliament == 38 & num_seconds > 6




* Figure 3: Relationship between number of seconds received and place on list
label variable seconds_received "Number of Seconds Received"

twoway (scatter seconds_received placeonlist, mcolor(%40) msize(small)) ///
	(lowess seconds_received placeonlist if P2P == 1, lcolor(red)) if parliament == 38, xline(87.5, lcolor(gs12)) title("38th Parliament") ///
	ytitle("Seconds Received") xtitle("Place on List") legend(label(1 "Number of Seconds")) legend(label(2 "Lowess"))
	graph save "./38_.gph"
twoway (scatter seconds_received placeonlist, mcolor(%40) msize(small)) ///
	(lowess seconds_received placeonlist if P2P == 1, ///
	lcolor(red)) if parliament == 39, xline(117.5, lcolor(gs12)) title("39th Parliament") ///
	ytitle("Seconds Received") xtitle("Place on List")
	graph save "./39_.gph"
twoway (scatter seconds_received placeonlist, mcolor(%40) msize(small)) ///
	(lowess seconds_received placeonlist if P2P == 1, lcolor(red)) if parliament == 40, xline(108.5, lcolor(gs12)) title("40th Parliament") ///
	ytitle("Seconds Received") xtitle("Place on List")
	graph save "./40_.gph"
twoway (scatter seconds_received placeonlist, mcolor(%40) msize(small)) ///
	(lowess seconds_received placeonlist if P2P == 1, lcolor(red)) if parliament == 41, xline(203.5, lcolor(gs12)) title("41st Parliament") ///
	ytitle("Seconds Received") xtitle("Place on List")
	graph save "./41_.gph"
twoway (scatter seconds_received placeonlist, mcolor(%40) msize(small)) ///
	(lowess seconds_received placeonlist if P2P == 1, lcolor(red)) if parliament == 42, xline(162.5, lcolor(gs12)) title("42nd Parliament") ///
	ytitle("Seconds Received") xtitle("Place on List")
	graph save "./42_.gph"
twoway (scatter seconds_received placeonlist, mcolor(%40) msize(small)) ///
	(lowess seconds_received placeonlist if P2P == 1, lcolor(red)) if parliament == 43, xline(61.5, lcolor(gs12)) title("43rd Parliament") ///
	ytitle("Seconds Received") xtitle("Place on List")
	graph save "./43_.gph"
grc1leg2 "38_" "39_" "40_" "41_" "42_" "43_", ycommon xcommon 


	
*Rescale place on list from 0 to 1
bysort parliament: egen min_placeonlist = min(placeonlist)
bysort parliament: egen max_placeonlist = max(placeonlist)
replace placeonlist = (placeonlist - min_placeonlist) / (max_placeonlist - min_placeonlist)

*descriptive stats on variables in the main model	
su seconder num_seconds seconds_received 

	
*Table 4: Regression Estimates of the Marginal Effects of Lottery Number of Seconds Given and Received
eststo clear
eststo: reg seconder c.placeonlist i.parliament
eststo: reg seconder c.placeonlist##c.placeonlist i.parliament
eststo: reg num_seconds c.placeonlist i.parliament
eststo: reg num_seconds c.placeonlist##c.placeonlist i.parliament
eststo: reg seconds_received c.placeonlist i.parliament if P2P == 1
eststo: reg seconds_received c.placeonlist##c.placeonlist i.parliament if P2P == 1
esttab using "table4.rtf",   ///
		label replace nogap nobaselevels compress b(3) se(3) drop(*parliament) 	///
		mtitles("seconded" "seconded" "given" "given" "received" "received") ///
		scalars("N N" "r2 R2" "r2_a R2 Adj.") 
			
	

*See "exogenous_dyad_replication" do file for Table 5



*********Appendix 
	
*Table B.1: Proposals with more cosponsors are more likely to be passed
eststo clear
eststo: reg passed2 seconds_received i.parliament 
esttab using "tableB1.rtf",   ///
		label replace nogap nobaselevels compress b(3) se(3) 	///
		mtitles("Passed") ///
		scalars("N N" "r2 R2" "r2_a R2 Adj.") 
	
*Table B.2: Regression Estimates of the Marginal Effects of Lottery Number of Seconds Given and Received with Controls	
eststo clear
eststo: reg seconder c.placeonlist i.parliament miss_lag num_seconds_lag_m
eststo: reg seconder c.placeonlist##c.placeonlist i.parliament miss_lag num_seconds_lag_m

eststo: reg num_seconds c.placeonlist i.parliament miss_lag num_seconds_lag_m
eststo: reg num_seconds c.placeonlist##c.placeonlist i.parliament miss_lag num_seconds_lag_m

eststo: reg seconds_received c.placeonlist i.parliament miss_lag num_seconds_lag_m if P2P == 1
eststo: reg seconds_received c.placeonlist##c.placeonlist i.parliament miss_lag num_seconds_lag_m if P2P == 1

esttab using "tableB2.rtf",   ///
		label replace nogap nobaselevels compress b(3) se(3) drop(*parliament)	///
		mtitles("seconded" "seconded" "given" "given" "received" "received") ///
		scalars("N N" "r2 R2" "r2_a R2 Adj.") 
		
*Table B.3: Main regressions with P2P	
eststo clear
eststo: reg seconder i.P2P i.parliament 
eststo: reg num_seconds i.P2P i.parliament 
eststo: reg seconds_received i.P2P i.parliament 
esttab using "tableB3.rtf",   ///
		label replace nogap nobaselevels compress b(3) se(3) drop(*parliament)	///
		mtitles("seconded" "given" "received") ///
		scalars("N N" "r2 R2" "r2_a R2 Adj.") 

*Table B.4: Main regressions with P2P and controls	
eststo clear
eststo: reg seconder i.P2P i.parliament miss_lag num_seconds_lag_m
eststo: reg num_seconds i.P2P i.parliament miss_lag num_seconds_lag_m
eststo: reg seconds_received i.P2P i.parliament miss_lag num_seconds_lag_m
esttab using "tableB4.rtf",   ///
		label replace nogap nobaselevels compress b(3) se(3) drop(*parliament)	///
		mtitles("seconded" "given" "received") ///
		scalars("N N" "r2 R2" "r2_a R2 Adj.") 		
		
*Table B.5: Regression Estimates of the Marginal Effects of Lottery Number of Seconds Given and Received
eststo clear
eststo: reg seconder c.placeonlist i.parliament, level(90)
*regress, level(90)
eststo: reg seconder c.placeonlist##c.placeonlist i.parliament, level(90)
eststo: reg num_seconds c.placeonlist i.parliament, level(90)
eststo: reg num_seconds c.placeonlist##c.placeonlist i.parliament, level(90)
eststo: reg seconds_received c.placeonlist i.parliament if P2P == 1, level(90)
eststo: reg seconds_received c.placeonlist##c.placeonlist i.parliament if P2P == 1, level(90)
esttab using "tableB5.rtf",   ///
		label replace ci level(90) nogap nobaselevels compress b(3)  	///
		mtitles("seconded" "seconded" "given" "given" "received" "received") ///
		scalars("N N" "r2 R2" "r2_a R2 Adj.") 


*See "analyses_dyad_replication" do file for Table B.6

*See "exogenous_dyad_replication" do file for Table B.7
